package main

import (
	"github.com/frankrap/huobi-api/hbdm"
	"log"
	"math"
)

// 计算 ATR
func (this *Trade) calc_atr(data []hbdm.SwapKLine) {
	bofu := 0.00
	l := len(data)
	for i := 0; i < l; i++ {
		v := data[i]
		bofu1 := v.High - v.Low
		bofu2 := bofu1
		bofu3 := bofu1
		if i < l-1 {
			// 今日最高价 - 昨日收盘价
			bofu2 = math.Abs(v.High - data[i+1].Close)
			// 昨日收盘价 - 今日最低价
			bofu3 = math.Abs(data[i+1].Close - v.Low)
		}
		current := math.Max(math.Max(bofu1, bofu2), bofu3)
		if bofu > 0 {
			bofu = (bofu + current) / 2
		} else {
			bofu = current
		}
	}
	log.Println("当前ATR值", bofu)
	this.ATR = bofu
}

// 计算 5个 5分钟线  5 个10分钟线
func (this *Trade) GetAverages(data []hbdm.SwapKLine) (fives [5]float64, tens [5]float64) {
	fives = [5]float64{}
	tens = [5]float64{}
	l := len(data)
	index := 0
	this.ClosePrices = make([]float64, 0)
	// 重置
	for i := l - 1; i >= 0; i-- {
		v := data[i]
		index++
		// 将15分钟 k线放入
		if index >= 1 && index < 6 {
			if fives[0] == 0 {
				fives[0] = v.Close
			} else {
				fives[0] = (fives[0] + v.Close) / 2
			}
		}
		if index >= 1 && index < 11 {
			if tens[0] == 0 {
				tens[0] = v.Close
			} else {
				tens[0] = (tens[0] + v.Close) / 2
			}
		}
		if index >= 2 && index < 7 {
			if fives[1] == 0 {
				fives[1] = v.Close
			} else {
				fives[1] = (fives[1] + v.Close) / 2
			}
		}
		if index >= 2 && index < 12 {
			if tens[1] == 0 {
				tens[1] = v.Close
			} else {
				tens[1] = (tens[1] + v.Close) / 2
			}
		}
		if index >= 3 && index < 8 {
			if fives[2] == 0 {
				fives[2] = v.Close
			} else {
				fives[2] = (fives[2] + v.Close) / 2
			}
		}
		if index >= 3 && index < 13 {
			if tens[2] == 0 {
				tens[2] = v.Close
			} else {
				tens[2] = (tens[2] + v.Close) / 2
			}
		}
		if index >= 4 && index < 9 {
			if fives[3] == 0 {
				fives[3] = v.Close
			} else {
				fives[3] = (fives[3] + v.Close) / 2
			}
		}
		if index >= 4 && index < 14 {
			if tens[3] == 0 {
				tens[3] = v.Close
			} else {
				tens[3] = (tens[3] + v.Close) / 2
			}
		}
		if index >= 5 && index < 10 {
			if fives[4] == 0 {
				fives[4] = v.Close
			} else {
				fives[4] = (fives[4] + v.Close) / 2
			}
		}
		if index >= 5 && index < 15 {
			if tens[4] == 0 {
				tens[4] = v.Close
			} else {
				tens[4] = (tens[4] + v.Close) / 2
			}
		}
		this.ClosePrices = append(this.ClosePrices, v.Close)
	}
	return
}

// 简单kline 平均线
func (this *Trade) SimpleAverage(data []hbdm.SwapKLine) [5]float64 {
	// closePrice := 0.00 // 平均收盘价
	l := len(data)
	fives, tens := this.GetAverages(data)
	current := data[l-1]

	// 默认震荡行情
	if fives[0] >= tens[0] &&
		fives[1] < tens[1] &&
		fives[2] < tens[2]-this.conf.AtrAvarage &&
		fives[3] < tens[3]-this.conf.AtrAvarage &&
		fives[4] < tens[4]-this.conf.AtrAvarage {
		// 5分钟均线突破向上趋势
		this.RecommendDirection = 0
		this.RecommendBuyPrice = tens[0]
		if this.RecommendBuyPrice > current.Close {
			this.RecommendBuyPrice = current.Close
		}
		log.Println("[5分钟均线突破向上趋势]", fives[0], tens[0])

	}

	if fives[0] <= tens[0] &&
		fives[1] > tens[1] &&
		fives[2] > tens[2]+this.conf.AtrAvarage &&
		fives[3] > tens[3]+this.conf.AtrAvarage &&
		fives[4] > tens[4]+this.conf.AtrAvarage {
		// 5分钟均线突破向下趋势
		this.RecommendDirection = 1
		this.RecommendSellPrice = tens[0]
		if current.Close > this.RecommendSellPrice {
			this.RecommendSellPrice = current.Close
		}
		log.Println("[5分钟均线突破向下趋势]", fives[0], tens[0])
	}
	return fives
}

// 简单kline 平均线
func (this *Trade) SimpleAverageHour(data []hbdm.SwapKLine) {
	closePrice := 0.00 // 平均收盘价
	l := len(data)
	index := 0
	// 重置
	for i := l - 1; i >= 0; i-- {
		index++
		if index < 100 {
			if closePrice <= 0 {
				closePrice = data[i].Close
			} else {
				closePrice = (closePrice + data[i].Close) / 2
			}
		}
		kline := data[l-i-1]
		// 将15分钟 k线放入
		this.HourClosePrices = append(this.HourClosePrices, kline.Close)
	}
	log.Println("[1小时顶部价格]", closePrice+7*this.ATR, "[1小时底部价格]", closePrice-3*this.ATR)
}
